package com.taiji.dicom4cloud.smart.handler;

import com.netsdk.lib.NetSDKLib;
import com.sun.jna.Pointer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

/**
 * 主动注册监听回调
 */
@Component
public class RegisterServiceCB implements NetSDKLib.fServiceCallBack  {

    //预警监听回调
    @Autowired
    private AlarmReceiveCB alarmReceiveCB;


    @Override
    public int invoke(NetSDKLib.LLong lHandle, String pIp, int wPort, int lCommand, Pointer pParam, int dwParamLen, Pointer dwUserData) {
        // 将 pParam 转化为序列号
        byte[] buffer = new byte[dwParamLen];
        pParam.read(0, buffer, 0, dwParamLen);
        try {
            final String deviceId = new String(buffer, "GBK").trim();
            switch(lCommand) {
                case NetSDKLib.EM_LISTEN_TYPE.NET_DVR_DISCONNECT: {  // 验证期间设备断线回调
                    break;
                }
                case NetSDKLib.EM_LISTEN_TYPE.NET_DVR_SERIAL_RETURN: { // 设备注册携带序列号
                    try {
                        Thread.sleep(20);
                    } catch (InterruptedException e1) {
                        e1.printStackTrace();
                    }
                    alarmReceiveCB.linkCamera(pIp,deviceId);
                    break;
                }
                default:
                    break;

            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
